package com.yunwushanhe.controller;

import com.yunwushanhe.annotation.SystemLog;
import com.yunwushanhe.bean.req.*;
import com.yunwushanhe.bean.ret.ResponseResult;
import com.yunwushanhe.bean.vo.*;
import com.yunwushanhe.entity.LoginUser;
import com.yunwushanhe.enums.AppHttpCodeEnum;
import com.yunwushanhe.exception.BlogSystemException;
import com.yunwushanhe.service.AdminUserService;
import com.yunwushanhe.service.MenuService;
import com.yunwushanhe.service.RoleService;
import com.yunwushanhe.util.BeanCopyUtil;
import com.yunwushanhe.util.UserContextUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 抬头看看天
 * @Date 2023-08-15  21:59:55
 */
@RestController
@RequestMapping
public class AdminUserController {

    @Resource
    private AdminUserService adminUserService;
    @Resource
    private MenuService menuService;
    @Resource
    private RoleService roleService;

    @PostMapping("/user/login") // 带请求体的请求，可以用 @RequestBody 注解来接收
    @SystemLog(businessName = "用户以后台管理员的身份进行登录的接口")
    public ResponseResult<Object> login(@RequestBody UserLoginReq userLoginReq){
        if(!StringUtils.hasText(userLoginReq.getUserName()) || !StringUtils.hasText(userLoginReq.getPassword())){
            throw new BlogSystemException(AppHttpCodeEnum.REQUIRE_USERNAME);
        }
        return adminUserService.adminLogin(userLoginReq);
    }

    @GetMapping("/getInfo")
    @SystemLog(businessName = "用户以后台管理员的身份进行登录时用于系统自动查询取其权限相关信息的接口")
    public ResponseResult<AdminUserInfoVO> getInfo(){
        // 获取当前登录用户的基本信息
        LoginUser user = UserContextUtil.getLoginUser();
        // java.lang.ClassCastException: java.lang.String cannot be cast to com.yunwushanhe.entity.LoginUser
        //只要上面这个异常以产生，多半就是上一行代码出了问题，意味着需要登录后才能进行这部操作。

        //根据用户id查询权限信息
        List<String> permList = menuService.selectPermListByUserId(user.getUser().getId());
        //根据用户id查询角色信息
        List<String> roleKeyList = roleService.selectRoleKeyListByUserId(user.getUser().getId());
        //获取用户信息
        UserInfoVO userInfoVO = BeanCopyUtil.copyBean(user.getUser(), UserInfoVO.class);
        //封装数据返回
        AdminUserInfoVO adminUserInfoVO = new AdminUserInfoVO(permList, roleKeyList, userInfoVO);
        return ResponseResult.ok(adminUserInfoVO);
    }

    @GetMapping("/getRouters")
    @SystemLog(businessName = "用户以后台管理员的身份进行登录时用于系统自动查询取其权限相关信息对应页面展示内容的接口")
    public ResponseResult<RouterListVO> getRouters(){
        Long userId = UserContextUtil.getUserId();
        return menuService.selectRouterMenuTreeByUserId(userId);
    }

    @PostMapping("/user/logout")
    @SystemLog(businessName = "登出接口")
    public ResponseResult<Object> logout(){
        return adminUserService.adminLogout();
    }

    @GetMapping("system/user/list")
    @SystemLog(businessName = "获取所有用户相关信息到页面进行分页显示的接口，这个用户目前感觉是普通用户。\n" +
                                "可以根据用户名模糊搜索；\n" +
                                "可以根据手机号的模糊搜索；\n" +
                                "可以根据状态查询。")
    public ResponseResult<PageVO<List<UserPageListVO>>> getUserPageList(UserPageListReq userPageListReq){
        return adminUserService.getUserPageList(userPageListReq);
    }

    @PostMapping("system/user")
    @SystemLog(businessName = "新增用户接口，通过这个加入的用户与注册加入的用户差不多，只不过通过这个接口加入可以直接关联角色")
    public ResponseResult<Object> addUser(@RequestBody UserAddReq userAddReq){
        return adminUserService.addUser(userAddReq);
    }

    @DeleteMapping("/system/user/{id}")
    @SystemLog(businessName = "删除用户的接口")
    public ResponseResult<Object> deleteUserById(@PathVariable("id") String id){
        return adminUserService.deleteUserById(id);
    }

    @GetMapping("/system/user/{id}")
    @SystemLog(businessName = "根据id查询用户信息回显接口")
    public ResponseResult<UserUpdateVO> getUserById(@PathVariable("id") Long id){
        return adminUserService.getUserById(id);
    }

    @PutMapping("/system/user")
    @SystemLog(businessName = "更新用户信息的接口")
    public ResponseResult<Object> updateUserById(@RequestBody UserUpdateReq userUpdateReq){
        return adminUserService.updateUserById(userUpdateReq);
    }

    @PutMapping("/system/user/changeStatus")
    @SystemLog(businessName = "简易改变用户状态的接口")
    public ResponseResult<Object> changeUserStatus(@RequestBody UserStatusUpdateReq userStatusUpdateReq){
        return adminUserService.changeUserStatus(userStatusUpdateReq);
    }

}
